﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

public partial class UserControl_GioHang_ChiTietGioHang : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            if (!Page.IsPostBack)
            {
                loaddata();
            }
        }
        catch (Exception)
        {
            
            throw;
        }
    }
    public void loaddata()
    {
        try
        {
            this.DataList1.DataSource = getGioHang();
            this.DataList1.DataBind(); 
        }
        catch (Exception)
        {
            
            throw;
        }
    }
    public DataTable getGioHang()
    {
        try
        {
             GioHang gh = new GioHang();
            gh = (GioHang)Session["GioHang"];
            DataTable dt = new DataTable();
            dt.Columns.Add("SanPhamID");
            dt.Columns.Add("SoLuong");
            dt.Columns.Add("Gia");
            dt.Columns.Add("ThanhTien");
            dt.Columns.Add("HinhAnh");
            foreach (ChiTietDonHang item in gh.List)
            {
                DataRow dr = dt.NewRow();
                dr[0] = item.Sanpham.Sanphamid;
                dr[1] = item.Soluong;
                dr[2] = item.Sanpham.Giaban;
                dr[3] = item.Thanhtien;
                dr[4] = item.Hinhanh.Url;
                dt.Rows.Add(dr);
            }
            this.lblTongThanhTien.Text = gh.TongThanhTien().ToString("N0") + " VNĐ";
            return dt;
        }
        catch (Exception)
        {
            
            throw;
        }
        
        
       }
    protected void txtSoLuong_TextChanged(object sender, EventArgs e)
    {
        
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        try
        {
            GioHang gh = new GioHang();
            gh = (GioHang)Session["GioHang"];
            for (int i = 0; i < gh.List.Count; i++)
            {
                gh.List[i].Soluong = Convert.ToInt32(((TextBox)this.DataList1.Items[i].FindControl("txtSoLuong")).Text);
            }
            Session["GioHang"] = gh;
            this.loaddata();
            //Response.Write(((TextBox)this.DataList1.Items[0].FindControl("txtSoLuong")).Text);
        }
        catch (Exception)
        {
            
            throw;
        }
       
    }
    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        try
        {
             GioHang gh = new GioHang();
            gh = (GioHang)Session["GioHang"];
            if(gh.List.Count>0)
            this.MultiView1.ActiveViewIndex = 1;
        }
        catch (Exception)
        {
            
            throw;
        }
        
            
    }
    protected void Button3_Click(object sender, EventArgs e)
    {
        try
        {
             Connection con = new Connection();
            SqlCommand com = new SqlCommand("donhang_insert", con.getCon());
            com.CommandType = CommandType.StoredProcedure;
            com.Parameters.Add("@HoTenKhach", SqlDbType.NVarChar, 250).Value = this.txtHoten.Text;
            com.Parameters.Add("@DiaChi", SqlDbType.NVarChar, 250).Value = this.txtDiachi.Text;
            com.Parameters.Add("@DienThoai", SqlDbType.VarChar, 20).Value = this.txtDienthoai.Text;
            com.Parameters.Add("@Email", SqlDbType.NVarChar, 50).Value = this.txtEmail.Text;
            com.Parameters.Add("@GhiChu", SqlDbType.NText).Value = this.txtGhichu.Text;
            con.openCon();
            com.ExecuteNonQuery();
           

            GioHang gh = new GioHang();
            gh = (GioHang)Session["GioHang"];
            foreach (ChiTietDonHang item in gh.List)
            {
                SqlCommand com1 = new SqlCommand("ChiTietDonHang_insert", con.getCon());
                com1.CommandType = CommandType.StoredProcedure;
                com1.Parameters.Add("@SanPhamID", SqlDbType.VarChar, 20).Value = item.Sanpham.Sanphamid;
                com1.Parameters.Add("@SoLuong", SqlDbType.Int).Value = item.Soluong;
                com1.Parameters.Add("Gia", SqlDbType.Float).Value = item.Sanpham.Giaban;
                com1.ExecuteNonQuery();
            }
            con.closeCon();
            this.MultiView1.ActiveViewIndex = 2;
        }
        catch (Exception ex)
        {
            
            throw new Exception("Khong Gui Duoc Don Hang" + ex.Message);
        }

        
    }
    protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
    {
        try
        {
             if (e.CommandName == "Xoa")
            {
                GioHang gh = new GioHang();
                gh = (GioHang)Session["GioHang"];
                gh.Delete(e.CommandArgument.ToString());
                Response.Write(e.CommandArgument.ToString());
                Session["GioHang"] = gh;
                this.loaddata();
            }
        }
        catch (Exception)
        {
            
            throw;
        }
    }
}